package com.dodock.banglapapers.server.helper;

import java.io.IOException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class DDNNewsDetailKalerkantho {

	String BASE = "http://www.kalerkantho.com/print_edition/index.php?view=details&type=gold&data=news&";
	private String IMAGE_BASE = "http://www.kalerkantho.com/print_edition/";

	public String Generate(String newsid) throws IOException {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		try {
			Entity detailsEntity = datastore.get(KeyFactory.createKey(
					"NewsDetails", "kalerkantho__" + newsid));
			Text text = (Text) detailsEntity.getProperty("data");
			String data = Settings.replaceExtras(text.getValue());
			return data;

		} catch (EntityNotFoundException e) {
			String url = BASE + newsid;
			Connection.Response response = Jsoup
					.connect(url)
					.userAgent(
							"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
					.timeout(10000).execute();

			Document doc = response.parse();
			JsonObject news = new JsonObject();
			news.addProperty("id", newsid);
			news.addProperty("sharelink", url);
			Element totalNews = doc.select("div[id=main_news]").first();
			if (totalNews != null) {
				JsonArray imageArray = new JsonArray();
				Elements galleryElements = totalNews
						.select("div[class=gallery clearfix]");
				if (!galleryElements.isEmpty()) {
					Element img = galleryElements.first();
					String image;
					Elements imgUrlObjs = img.select("a[rel=prettyPhoto]");
					if (!imgUrlObjs.isEmpty()) {
						Element imgUrlObj = imgUrlObjs.first();
						if (imgUrlObj != null) {
							image = imgUrlObj.attr("href").trim();
							JsonObject obj = new JsonObject();
							obj.addProperty("url", IMAGE_BASE + image);
							imageArray.add(obj);
							news.add("gallery", imageArray);
						}
					}
				}
				Element newsDetailElement = totalNews.select(
						"div[id=newsDtlSummary]").first();
				Elements newsDetailsObj = newsDetailElement
						.select("span[style=display:block; text-align:justify; font-size:15px; color:#1A1A1A]");
				String details = "";
				if (!newsDetailsObj.isEmpty()) {
					Element obj = newsDetailsObj.first();
					obj = obj.after("div[class=all_ss gads]");
					details = obj.text();
					if (details != null) {
						details = details.replaceAll("\u00a0", "<br/>");
					}
				}
				news.addProperty("details", details);
				String data = news.toString();
				data = Settings.replaceExtras(data);
				Text text = new Text(data);
				/* save here */
				Entity detailsEntity = new Entity("NewsDetails",
						"kalerkantho__" + newsid);
				detailsEntity.setProperty("data", text);
				datastore.put(detailsEntity);
				return data;
			} else {
				return "{}";
			}
		}
	}

}
